package com.ty.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ty.entity.SysAdmin;
import com.ty.entity.SysRole;
import com.ty.service.MenusService;
import com.ty.service.RoleService;
import com.ty.util.Pager;

@Controller
@RequestMapping("/admin/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private MenusService menusService;
	
	@RequestMapping("/index")
	public String index() {
		return "admin/role/index";
	}
	
	
	//2.列表页
	@RequestMapping("/indexList")
	public String indexList(int pageNum,int pageSize,SysRole role,ModelMap model) {
		Pager pager= roleService.selectByPager(pageNum, pageSize, role);
		model.put("pager", pager);
		return "admin/role/indexList";
	} 
	
	
	//5.保存或更新
		@RequestMapping("/saveOrUpdate")
		@ResponseBody
		public Map<String, Object> saveOrUpdate(SysRole role,int isUpdate){
			 boolean flag=false;
			 if(isUpdate==1) { //更新操作
				 flag=roleService.updateByObj(role);
			 }else {  //保存操作
				 flag=roleService.saveObject(role);
			 }
			 Map<String, Object> returnJson=new HashMap<>();
			 if(flag) {
				 returnJson.put("status", "1"); //操作成功
				 returnJson.put("msg", "操作成功！");
			 }else {
				 returnJson.put("status", "0"); //操作失败
				 returnJson.put("msg", "操作失败！");
			 }
			 return returnJson;
			
		}
		
	
	//6.跳转到更新或保存页面
	@RequestMapping("/get")
	public String get(Long id,ModelMap model) {
		if(id!=null) { //调整到更新
			SysRole role= roleService.getById(id);
			model.put("m",role);
			model.put("isUpdate", 1); //标记当前页面是更新
		}else {
			model.put("isUpdate", 0);//标记当前页面是保存
		}
		
		return "admin/role/saveOrUpdate";
	}
	
	
	//3.单个删除
	@RequestMapping("/deleteOne")
	@ResponseBody
	public Map<String,Object> deleteOne(Long id){
		boolean flag= roleService.deleteById(id);
		Map<String, Object> returnJson=new HashMap<>();
		if(flag) {
			returnJson.put("status", 1);
			returnJson.put("msg","操作成功！");
		}else {
			returnJson.put("status", 0);
			returnJson.put("msg","操作失败！");
		}
		return returnJson;
	}
	
	//4.多个删除
	@RequestMapping("/batchDelete")
	@ResponseBody
	public Map<String,Object> batchDelete(String ids){
		boolean flag= roleService.deleteBatch(ids);
		Map<String, Object> returnJson=new HashMap<>();
		if(flag) {
			returnJson.put("status", 1);
			returnJson.put("msg","操作成功！");
		}else {
			returnJson.put("status", 0);
			returnJson.put("msg","操作失败！");
		}
		return returnJson;
	}
	
	
	@RequestMapping("/persMenus")
	public String persMenus(Long roleId,ModelMap model) {
	    SysRole role=roleService.getById(roleId);
	    model.put("m", role);
	    return "/admin/role/persMenus";
	}
	
	/**
	 * 获取角色对应的菜单
	 * @return
	 */
	@RequestMapping("/getRoleMenus")
	@ResponseBody
	public List<Map<String,Object>> getRoleMenus(Long roleId) {
		List<Map<String,Object>> list=menusService.getRoleMenus(roleId);
		return list;
	}
	
	/**
	 * 保存角色对于菜单
	 * @return
	 */
	@RequestMapping("/saveRoleMenus")
	@ResponseBody
	public Map<String,Object> saveRoleMenus(String menusId,Long roleId){
		 SysRole sysRole= roleService.getById(roleId);
		 sysRole.setMenus(menusId);
		 boolean flag= roleService.updateByObj(sysRole);
		 Map<String, Object> returnJson=new HashMap<>();
		 if(flag) {
			returnJson.put("status", 1);
			returnJson.put("msg","操作成功！");
		 }else {
			returnJson.put("status", 0);
			returnJson.put("msg","操作失败！");
		 }
		return returnJson;
	}
}
